﻿<%@ Page Language="C#" %>
<%@ Import Namespace="Findy.XsltDb" %>

<script runat="server">
/*
 XsltDb is powerful XSLT module for DotnetNuke.
 It offers safe database access, SEO-friendly AJAX support,
 visitor interactions, environment integration (dnn properties,
 request, cookie, session and form values), regular expressions, etc.

 Author:
 
    Anton Burtsev
    burtsev@yandex.ru

 Project home page: 
 
    http://xsltdb.codeplex.com
*/
    
    protected void Page_Load(object sender, EventArgs e)
    {
        XsltDbUtils.UpdateCulture();

        Response.ContentType = "application/javascript";
        
        var encoding = (Request["encoding"] ?? string.Empty).Trim();
        var enc = Encoding.UTF8;
        try
        {
            if (encoding.Length != 0)
            {
                int codepage;
                enc = int.TryParse(encoding, out codepage) 
                    ? Encoding.GetEncoding(codepage) 
                    : Encoding.GetEncoding(encoding);
            }
        }
        catch (Exception ex)
        {
            XsltDbUtils.WriteLog(ex);
        }

        Response.ContentEncoding = enc;

        var res = string.Empty;
        try
        {
            res = Execute();
        }
        catch (Exception ex)
        {
            if ( UserController.GetCurrentUserInfo().IsSuperUser )
                res = XsltDbUtils.GetExceptionMessage(ex);
        }

        var code = string.Format("var {0} = {1};", Request["var"], Transformer.EncodeJsString(res));

        Response.Write(code);
        Response.End();
    }

    private string Execute()
    {
        var ModuleID = Convert.ToInt32(Request["mod"]);

        var mc = new DotNetNuke.Entities.Modules.ModuleController();
        var mi = mc.GetModule(ModuleID);

        var t = new Transformer(mi.TabModuleID, ModuleID, mi.TabID);

        var xml = @"<root/>";

        var c = XsltDbUtils.GetConfig(ModuleID);
        t.Config = c;
        var xsl = c.ActiveXslt;
        return xsl.Trim().Length > 0
                         ? t.Transform(xml, xsl, c.IsSuper)
                         : "Use <b>Edit XSLT</b> link to setup an XSL transformation.";
    }
</script>